<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="货物名称" prop="goodsId">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入货物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物批次号" prop="batchNum" label-width="120px">
        <el-input
          v-model="queryParams.batchNum"
          placeholder="请输入货物批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报告名称" prop="checkName">
        <el-input
          v-model="queryParams.checkName"
          placeholder="请输入报告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="质检结果" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择质检结果" clearable>
          <el-option
            label="合格"
            value="0"
          />
          <el-option
            label="不合格"
            value="1"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="质检日期" prop="checkTime">
        <el-date-picker
          v-model="createTime"
          type="daterange"
          align="right"
          unlink-panels
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['source:check:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="checkId" />
      <el-table-column label="报告名称" align="center" prop="checkName" />
      <el-table-column label="货物名称" align="center" prop="goodsName" />
      <el-table-column label="批次号" align="center" prop="batchNum" />
      <el-table-column label="质检结果" align="center" prop="status" >
        <template v-slot="scope">
          <el-tag v-if="scope.row.status === '0'" type="success">合格</el-tag>
          <el-tag v-if="scope.row.status === '1'" type="danger">不合格</el-tag>
          <el-tag v-if="scope.row.status === '2'" type="warning">未审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="质检时间" align="center" prop="checkTime" width="180" />
      <el-table-column label="检测员" align="center" prop="inspectorsName" />
      <el-table-column label="批准人" align="center" prop="ratifyPeople" />
      <el-table-column label="检测方法" align="center" prop="checkType" />
      <el-table-column label="质检图片" align="center" prop="img">
        <template v-slot="scope">
          <el-image :src="scope.row.img" :preview-src-list="[scope.row.img]" style="width: 100px; height: 100px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >审批</el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改货物质检对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="货物名称" prop="goodsName" >
          <el-input v-model="form.goodsName" placeholder="请输入货物名称" disabled/>
        </el-form-item>
        <el-form-item label="货物批次号" prop="batchNum" >
          <el-input v-model="form.batchNum" placeholder="请输入货物批次号" disabled/>
        </el-form-item>
        <el-form-item label="报告名称" prop="checkName">
          <el-input v-model="form.checkName" placeholder="请输入质检名称" />
        </el-form-item>
        <el-form-item label="质检人" prop="inspectors">
          <el-input v-model="form.inspectors" placeholder="请输入质检人" />
        </el-form-item>
        <el-form-item label="质检方式" prop="checkType">
          <el-input v-model="form.checkType" placeholder="请输入质检方式" />
        </el-form-item>
        <el-form-item label="质检图片" prop="img">
          <el-upload
            class="avatar-uploader"
            :action=upload.url
            :headers = upload.headers
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item  label="质检结果"  prop="checkType">
          <el-radio  v-model="form.status" label="0">通过</el-radio>
          <el-radio v-model="form.status" label="1">不通过</el-radio>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { SanjiList,listRecord, delRecord, updateRecord,tongyiYes,tongyiNo} from "@/api/source/check/check";
import {getToken} from "@/utils/auth";

export default {
  name: "Record",
  data() {
    return {
      createTime:[],
      //上传图片地址
      upload: {
        headers: { Authorization: "Bearer " + getToken()},
        url: process.env.VUE_APP_BASE_API + "/source/crop/file/upload"
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货物质检表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        checkName: null,
        status: null,
        img: null,
        isFault: null
      },
      // 表单参数
      form: {
        goodsName:'',
        batchNum:'',
      },
      // 表单校验
      rules: {
        checkName: [
          { required: true, message: "报告名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "质检结果不能为空", trigger: "blur" }
        ],
        img: [
          { required: true, message: "质检图片不能为空", trigger: "blur" }
        ],
        checkTime: [
          { required: true, message: "质检时间不能为空", trigger: "blur" }
        ],
        inspectors: [
          { required: true, message: "质检人不能为空", trigger: "blur" }
        ],
        ratifyPeople: [
          { required: true, message: "批准人不能为空", trigger: "blur" }
        ],
        checkType: [
          { required: true, message: "质检方式不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],

      },
      formLabelWidth:"80px",
      imageUrl:'',
    };
  },
  created() {
    this.getList();

  },
  methods: {
    mounted() {
      this.onSubmit();
      this.bt();
      this.init();
    },
    //图片
    handleAvatarSuccess(res,file){
      this.imageUrl = file.response.msg;
      this.form.img=file.response.msg;
      console.log(file);
    },
    /** 查询货物质检列表 */
    getList() {
      this.queryParams.startTime=this.createTime[0];
      this.queryParams.endTime=this.createTime[1];
      console.log(this.createTime);
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkId: null,
        goodsId: null,
        checkNum: null,
        status: null,
        img: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isFault: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加货物质检";
    //   this.imageUrl='';
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改货物质检";
      this.form=row;
      console.log(this.form);
      this.imageUrl=this.form.img;
      this.form.goodsName=row.goodsName;
      this.form.batchNum=row.batchNum;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkId != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
          // else {
          //   addRecord(this.form).then(response => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const checkIds = row.checkId || this.ids;
      this.$modal.confirm('是否确认删除货物质检编号为"' + checkIds + '"的数据项？').then(function() {
        return delRecord(checkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('system/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    },

  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
